package logic

import (
	"context"
	"three-sxp/common"
	"three-sxp/models/mysql"
	"three-sxp/orderrpc/orderrpcclient"

	"three-sxp/api/internal/svc"
	"three-sxp/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PayLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PayLogic {
	return &PayLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PayLogic) Pay(req *types.PayRequest) (resp *types.PayResponse, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.OrderRpc.Pay(l.ctx, &orderrpcclient.PayRequest{
		OrderId: int64(req.OrderId),
	})
	if res.Code != 200 {
		return &types.PayResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
		}, nil
	}
	var order mysql.Order
	order.UserSn = res.OrderSn
	order.Money = float64(res.Money)
	ali := new(common.AliPay)
	url, err := ali.Payment(order)
	if err != nil {
		return &types.PayResponse{
			Code: 400,
			Msg:  "支付失败",
		}, nil
	}
	return &types.PayResponse{
		Code: int(res.Code),
		Msg:  res.Msg,
		Url:  url,
	}, nil
}
